﻿using UnityEngine;
using System.Collections;
using UI;
using Base.Extensions;
public class 模型 : BasePanel
{
    private bool isui = false;
    private CanvasGroup group;
    private int sum = 0;
    private RectTransform rect;
    private RectTransform canvas;
    // Use this for initialization
    void Awake()
    {
        group = GetComponent<CanvasGroup>();
        canvas = GameObject.Find("Canvas").GetComponent<RectTransform>();
        rect = gameObject.GetComponent<RectTransform>();
    }

    public override void OnEnter()
    {
        sum = 0;
        isui = false;
        gameObject.SetActive(true);
        transform.position = Input.mousePosition;
        Vector3 posin = transform.localPosition;
        float x = posin.x;
        float y = posin.y;
        if (x < -(canvas.sizeDelta.x / 2 - rect.sizeDelta.x / 2))
        {
            x = -(canvas.sizeDelta.x / 2 - rect.sizeDelta.x / 2);
        }
        else if (x > canvas.sizeDelta.x / 2 - rect.sizeDelta.x / 2)
        {
            x = canvas.sizeDelta.x / 2 - rect.sizeDelta.x / 2;
        }
        if (y < -(canvas.sizeDelta.y / 2 - rect.sizeDelta.y / 2))
        {
            y = -(canvas.sizeDelta.y / 2 - rect.sizeDelta.y / 2);
        }
        else if (y > canvas.sizeDelta.y / 2 - rect.sizeDelta.y / 2)
        {
            y = (canvas.sizeDelta.y / 2 - rect.sizeDelta.y / 2);
        }
        transform.localPosition = new Vector3(x, y, 0);
    }
    public override void OnExit()
    {

        gameObject.SetActive(false);
    }
    public override void OnPause()
    {
        isui = true;
    }
    public override void OnResume()
    {
        isui = false;
    }
    // Update is called once per frame
    void Update()
    {

        if (!isui)
        {
            if (Input.GetMouseButtonDown(0))
            {
                StartCoroutine(延迟关闭(0.2f));
            }
        }
    }
    IEnumerator 延迟关闭(float item)
    {
        yield return new WaitForSeconds(item);
        if (!isui)
        {
            UIManager.Instance.PopPanel();
        }
    }
    public void 按钮点击事件(string 按钮名字)
    {
            UIManager.Instance.PopPanel(this);
        if (按钮名字 == "报修清单" || 按钮名字== "保养清单")
            UIManager.Instance.PopPanel();
        else
            UIManager.Instance.PopPanel(this);
        UIPanelType panelType = (UIPanelType)System.Enum.Parse(typeof(UIPanelType), 按钮名字);
        UIManager.Instance.PushPanel(panelType);
    }
}
